package com.db;

import java.io.Serializable;
import java.net.UnknownHostException;
import java.util.ArrayList;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class DictionaryDB {

	static private DictionaryDB dictionaryDB = null;
	
	public static DictionaryDB getInstance() {
		if(dictionaryDB == null)
			dictionaryDB = new DictionaryDB();
		return dictionaryDB;
	}

	private Mongo mongoLab;
	DB shortTranslateDB;
	DBCollection dictionaryCollection;
	
	public DBCollection getDictionaryCollection() {
		return dictionaryCollection;
	}
	
	public DictionaryDB() {
		connect();
	}
	
	public void connect()  {
		
		try {
			mongoLab = new Mongo( "dbh13.mongolab.com" , 27137 );
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (MongoException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		shortTranslateDB = mongoLab.getDB("short_translate");
 
		try{
			if(!shortTranslateDB.authenticate("short_translate", "admin".toCharArray())) {
				throw new Exception("DB Authentication failed.");
			}
			
			dictionaryCollection = shortTranslateDB.getCollection("dictionary");
				
		}
		catch(Exception e) {
			System.err.append(e.getMessage());
		}
		
		
	}
	
	public void insertWord(String word, String wordLanguage, String translationLanguage, String[] translations) {
		 
		BasicDBObject doc = new BasicDBObject();
	 	doc.put("word", word);
	 	doc.put("wordLanguage", wordLanguage);
       
        
	 	BasicDBObject doc2 = (BasicDBObject) getDictionaryCollection().findOne( doc );
	  
  
        if(doc2 == null){
        	
        	doc.put(translationLanguage, translations);
        	getDictionaryCollection().save(doc);
        }
        
        else {
        	doc2.put(translationLanguage, translations);
        	getDictionaryCollection().update(doc, doc2);
        }
         
		 
	}
	
	public ArrayList<String> queryLanguageWords(String language) {
		
		
		 BasicDBObject query = new BasicDBObject();

	     query.put("wordLanguage", language );
 
	     DBCursor cur = getDictionaryCollection().find(query, new BasicDBObject("word", 1).append("_id", 0));
	     
	     ArrayList<String> queryResults = new ArrayList<String>();

	     
	     while(cur.hasNext()) {
	    	 queryResults.add((String)cur.next().get("word"));
	    	 
	     }
	     
		 return queryResults;
	}
	
	public ArrayList<String> queryWordTranslation(String word, String language, String translationLanguage) {
		
		BasicDBObject query = new BasicDBObject();

		query.put("word", word );
		query.put("wordLanguage", language );
		DBCursor cur = getDictionaryCollection().find(query, new BasicDBObject(translationLanguage, 1).append("_id", 0));
		
		ArrayList<String> queryResults = new ArrayList<String>();
		 
		
		BasicDBList dbList = new BasicDBList();
		
		if(cur.hasNext()) {
			
			dbList = (BasicDBList) cur.next().get(translationLanguage);
			
			if(dbList == null) {
				queryResults.add("Translation not found");
			}
			
			else {
				for(int f = 0; f < dbList.size(); f++) {
					queryResults.add(dbList.get(f).toString());
				}
			}
			
		}
		
		else {
			queryResults.add("Word not found");
		}
	
		return queryResults;
	}
	
	
}
